USE [influenza]
GO

/****** Object:  StoredProcedure [dbo].[SP_RptFaseAnalitica]    Script Date: 03/12/2010 17:53:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


create PROCEDURE [dbo].[SP_RptFaseAnalitica]
@param varchar(100)

as
Begin
declare 
@dias int,
@qry varchar(8000)

Set @qry='

SELECT id_laboratorio,datediff(day,fRecepcion,fRes) as dia
FROM dat_laboratorio a
INNER JOIN (select id_dat_laboratorio,max(fResultado) as fRes
			from aux_laboratorio_res 
			group by id_dat_laboratorio) b ON (a.id_principal = b.id_dat_laboratorio)'
if @param >0
begin
	Set @qry=@qry+' Where id_Laboratorio='+@param
end
Set @qry=@qry+' order by dia'

exec (@qry)

end
GO

